#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

int main()
{
    struct student
    {
        /* data */
        int id;
        std::string name;
        std::string sex;
    };
    // 序列化
    struct student stu = {18, "sherry", "male"};
    Json::Value root_writer;
    root_writer["id"] = stu.id;
    root_writer["name"] = stu.name;
    root_writer["sex"] = stu.sex;

    // Json::StyledWriter writer;
    Json::FastWriter writer;
    std::string json_string = writer.write(root_writer);
    std::cout << json_string << std::endl;

    // 反序列化
    struct student stu_read;
    Json::Value root_reader;
    Json::Reader reader;
    reader.parse(json_string, root_reader);
    stu_read.id = root_reader["id"].asInt();
    stu.name = root_reader["name"].asString();
    stu.sex = root_reader["sex"].asString();
    std::cout << stu_read.id << std::endl;
    std::cout << stu.name << std::endl;
    std::cout << stu.sex << std::endl;

    return 0;
}